//
//  WNBaseNavigationController.m
//  iOSOCProjectDirectory
//
//  Created by BHmac on 2017/6/8.
//  Copyright © 2017年 XieXuan. All rights reserved.
//

#import "WNBaseNavigationController.h"

@interface WNBaseNavigationController ()<UIGestureRecognizerDelegate>

@end

@implementation WNBaseNavigationController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    // 设置导航栏属性
    [self setUpNavigtionController];
    
    // 手势管理
    self.interactivePopGestureRecognizer.delegate = self;
    
}

- (void)setUpNavigtionController{

    // 设置导航栏背景颜色
    self.navigationBar.barTintColor = [UIColor orangeColor];
    self.view.backgroundColor = [UIColor whiteColor];
    
    // 禁止 navigationBar 和 tabBar 为透明
    self.navigationController.navigationBar.translucent = NO;
    self.tabBarController.tabBar.translucent = NO;
    
}

// 重写push方法
- (void)pushViewController:(UIViewController *)viewController animated:(BOOL)animated{
    // childViewControllers 子视图数组
    [super pushViewController:viewController animated:animated];
    
    if (self.childViewControllers.count > 1) {
        
        // 设置返回按钮
        UIButton *button = [UIButton buttonWithType:UIButtonTypeSystem];
        [button setImage:[UIImage imageNamed:@"ico_return"] forState:UIControlStateNormal];
        [button setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        [button sizeToFit];
        [button addTarget:self action:@selector(returnButton:) forControlEvents:UIControlEventTouchUpInside];
        viewController.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc]initWithCustomView:button];
        // 隐藏底部标签栏
        viewController.hidesBottomBarWhenPushed = NO;
    }
    
}

- (void)returnButton:(UIButton *)sender{
    
    [self popViewControllerAnimated:YES];
    
}

#pragma mark -------------------------------------------- 手势处理
- (BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer *)gestureRecognizer{
    // 手势何时有效：当子控制器的个数 > 1
    return self.childViewControllers.count > 1;
}

@end
